﻿Imports System.IO
Imports System.Threading
Imports iUtil
Public Class Form1


    Structure params
    Dim scan As Boolean
    Dim filename As String
    End Structure

    Public WithEvents iAntiClass As iUtil.AntiVirus

    Private Sub scnAutorun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles scnAutorun.Click
        lstListBox.Items.Clear()
        Dim pr As New params With {.scan = True, .filename = "autorun.inf"}
        Dim thrd As New System.Threading.Thread(AddressOf GetDrives)
        thrd.Start(pr)
    End Sub



    Public Sub GetDrives(ByVal para As DriveType)
        For Each drive As DriveInfo In DriveInfo.GetDrives
            If drive.DriveType = para Then
                If drive.IsReady = True Then
                    'listbox.Items.Add(drive.Name)
                    AddToListBox(drive.Name)
                End If
            End If
        Next drive
    End Sub

    Private Delegate Sub AddDelegate(ByVal drive As String)
    Private Sub AddToListBox(ByVal drive As String)
        If lstListBox.InvokeRequired Then
            Invoke(New AddDelegate(AddressOf AddToListBox), New Object() {drive})
        Else
            lstListBox.Items.Add(drive)
        End If
    End Sub

    Private Delegate Sub ScanDelegate(ByVal drive As String, ByVal filename As String)
    Private Sub Scan(ByVal drive As String, ByVal filename As String)
        If InvokeRequired Then
            Invoke(New ScanDelegate(AddressOf Scan), New Object() {drive, filename})
        Else
            If File.Exists(drive & filename) = True Then
                MessageBox.Show("Virus found")
                Dim sr As New StreamReader(drive & filename)
                txtTEXT.Text = sr.ReadToEnd
                sr.Close()
            End If
        End If
    End Sub



    Private Sub btnButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton2.Click
        lstListBox.Items.Clear()
        Dim thrd As New System.Threading.Thread(AddressOf GetDrives)
        thrd.Start(DriveType.Removable)
    End Sub

    Private Sub btnRECYCLING_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRECYCLING.Click
        If Not lstListBox.Text = String.Empty Then
            Dim iAntiVirus As New iUtil.AntiVirus
            Dim pr As New iUtil.AntiVirus.FolderParams With {.path = lstListBox.Text, .scanSub = True}
            iAntiVirus.ScanFolder(pr)
        Else
            MessageBox.Show("Not a valid path selected")
        End If
    End Sub


    'Custom function for VT.
    Private Sub RemoveINI()
        Dim folder As New FolderBrowserDialog
        folder.ShowDialog()
        Dim selPath As String = folder.SelectedPath
        If Not selPath = String.Empty Then
            Dim iAntivirus As New iUtil.AntiVirus
            Dim pr As New iUtil.AntiVirus.FolderParams With {.path = selPath, .scanSub = False}
            iAntivirus.ScanFolder(pr)
        Else
            MessageBox.Show("Not a valid path selected")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVT.Click
        'Dim iAntiPara As New iUtil.AntiVirus.FolderParams With {.path = "D:\", .scanSub = True}
        'iAntiClass = New iUtil.AntiVirus
        'Dim thrd As New System.Threading.Thread(AddressOf iAntiClass.ScanFolder)
        'thrd.Start(iAntiPara)

        Try
            Dim folder As New FolderBrowserDialog
            folder.ShowDialog()
            Dim iAntiPara As New iUtil.AntiVirus.FilesParams With {.path = folder.SelectedPath, .scanSub = True, .FileSpec = "*.*"}
            iAntiClass = New iUtil.AntiVirus
            Dim thrd As New System.Threading.Thread(AddressOf iAntiClass.ScanFiles)
            thrd.Start(iAntiPara)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub Started() Handles iAntiClass.Started
        Dim txtTEXT As String = ""
        txtTEXT += vbCrLf & "Scanning started." & vbCrLf
        Inject(txtTEXT)
    End Sub

    Private Sub completed() Handles iAntiClass.Completed
        Dim txtTEXT As String = ""
        txtTEXT += vbCrLf & "Scanning completed." & vbCrLf
        Inject(txtTEXT)
    End Sub

    Private Sub VirusFound(ByVal path As String, ByVal type As String, ByVal details As String) Handles iAntiClass.VirusFound
        Dim txtTEXT As String = ""
        txtTEXT += vbCrLf & type & ": " & path & vbCrLf & details & vbCrLf
        Inject(txtTEXT)
    End Sub

    Private Delegate Sub DelInject(ByRef text As String)
    Private Sub Inject(ByRef text As String)
        If txtTEXT.InvokeRequired Then
            Invoke(New DelInject(AddressOf Inject), New Object() {Text})
        Else
            txtTEXT.Text += text
        End If
    End Sub

    Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click
        Dim iAntiPara As New iUtil.AntiVirus.FolderParams With {.path = "D:\Tools\", .scanSub = True}
        iAntiClass = New iUtil.AntiVirus
        Dim thrd As New System.Threading.Thread(AddressOf iAntiClass.ScanFolder)
        thrd.Start(iAntiPara)
    End Sub

    Private Sub btnLogicalDrives_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogicalDrives.Click
        lstListBox.Items.Clear()
        Dim thrd As New Thread(AddressOf GetDrives)
        thrd.Start(DriveType.Fixed)
    End Sub
End Class
